/*	
	SCEP-Series Graphic engine

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    
*/
#include <string.h>
#include <pspdisplay.h>
#include <pspsysmem_kernel.h>

#include "graphic.h"

#define A(color) (u8)(color >> 24 & 0xFF)
#define B(color) (u8)(color >> 16 & 0xFF)
#define G(color) (u8)(color >> 8 & 0xFF)
#define R(color) (u8)(color & 0xFF)
#define BGR(color) (color & 0x00FFFFFF)
#define ABGR(a, b, g, r) (((u8)a << 24) | ((u8)b << 16) | ((u8)g << 8) | (u8)r)
#define IS_FLOAT(y) ((y - (int)y) > 0? true: false)
#define IS_ALPHA(color) ((color & 0xFF000000) == 0xFF000000? false: true)

static u32 Gcolor[] = 
{
	0x80ECA355, /* Rectangle */
	0xC0FFFFFF, /* Text */
	0x804D4D4D, /* Line */
	0xFF000000, /* Text shadow */
	0x02EC5D55, /* Rect shadow */
};

static int line_height = 15;

static SceUID heapid = -1;
static int bufferwidth, pixelformat, unk, buffersize, pixelsize;
static Point p0, p1;
static void * vram, * vram_buf;

static u8 * r, * g, * b;
static int gauss_width = 7;
static int gauss_fact[] = {1, 6, 15, 20, 15, 6, 1};
static int gauss_sum = 64;

static u8 fontp8[] = {
	0x24, 0x00, 0x28, 0x00, 0x3c, 0x00, 0x49, 0x00, 0x6d, 0x00, 0x99, 0x00, 0xc5, 0x00, 0xf1, 0x00, 
	0xfb, 0x00, 0x27, 0x01, 0x53, 0x01, 0x6b, 0x01, 0x87, 0x01, 0x95, 0x01, 0x9c, 0x01, 0xa4, 0x01, 
	0xd0, 0x01, 0xfc, 0x01, 0x18, 0x02, 0x3c, 0x02, 0x60, 0x02, 0x84, 0x02, 0xa8, 0x02, 0xd4, 0x02, 
	0xf8, 0x02, 0x1c, 0x03, 0x48, 0x03, 0x58, 0x03, 0x6e, 0x03, 0x8a, 0x03, 0x9a, 0x03, 0xb6, 0x03, 
	0xda, 0x03, 0x06, 0x04, 0x32, 0x04, 0x56, 0x04, 0x7a, 0x04, 0x9e, 0x04, 0xc2, 0x04, 0xe6, 0x04, 
	0x0a, 0x05, 0x2e, 0x05, 0x4a, 0x05, 0x6e, 0x05, 0x92, 0x05, 0xb6, 0x05, 0xe2, 0x05, 0x06, 0x06, 
	0x32, 0x06, 0x56, 0x06, 0x8c, 0x06, 0xb8, 0x06, 0xdc, 0x06, 0x00, 0x07, 0x24, 0x07, 0x50, 0x07, 
	0x7c, 0x07, 0xa8, 0x07, 0xd4, 0x07, 0xf8, 0x07, 0x28, 0x08, 0x54, 0x08, 0x84, 0x08, 0xa0, 0x08, 
	0xa8, 0x08, 0xb0, 0x08, 0xcc, 0x08, 0xf0, 0x08, 0x0c, 0x09, 0x30, 0x09, 0x4c, 0x09, 0x70, 0x09, 
	0x94, 0x09, 0xb8, 0x09, 0xdc, 0x09, 0x08, 0x0a, 0x2c, 0x0a, 0x50, 0x0a, 0x72, 0x0a, 0x8e, 0x0a, 
	0xb0, 0x0a, 0xd4, 0x0a, 0xf8, 0x0a, 0x14, 0x0b, 0x30, 0x0b, 0x54, 0x0b, 0x70, 0x0b, 0x92, 0x0b, 
	0xb4, 0x0b, 0xd6, 0x0b, 0xfa, 0x0b, 0x10, 0x0c, 0x40, 0x0c, 0x54, 0x0c, 0x84, 0x0c, 0x00, 0x00, 
};

static u16 * fontp = (u16 *)fontp8;

static s8 font[] = {
	0x08, 0x07, 0x08, 0x00, 0x4f, 0xcc, 0xdf, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 
	0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 
	0x4f, 0xcc, 0xdf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x08, 0x03, 0x08, 0x03, 0x4f, 0x10, 0x4f, 0x10, 
	0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x1a, 0x00, 0x2d, 0x00, 0x03, 0x06, 0x08, 0x01, 
	0x1f, 0x71, 0xf6, 0x0d, 0x50, 0xe4, 0x0a, 0x20, 0xb1, 0x08, 0x07, 0x08, 0x00, 0x00, 0x3a, 0x06, 
	0x70, 0x00, 0x94, 0x0c, 0x10, 0xcc, 0xec, 0xde, 0xc0, 0x03, 0xa0, 0x76, 0x00, 0x08, 0x50, 0xb2, 
	0x00, 0xce, 0xcc, 0xec, 0xc0, 0x2b, 0x06, 0x70, 0x00, 0x76, 0x0c, 0x20, 0x00, 0x0a, 0x08, 0x09, 
	0x00, 0x00, 0x00, 0xe0, 0x00, 0x04, 0xbc, 0xec, 0xa0, 0x2e, 0x50, 0xe0, 0x00, 0x2e, 0x80, 0xe0, 
	0x00, 0x03, 0xbe, 0xe1, 0x00, 0x00, 0x02, 0xed, 0x70, 0x00, 0x00, 0xe6, 0xe0, 0x12, 0x00, 0xe6, 
	0xd0, 0x2b, 0xcc, 0xeb, 0x20, 0x00, 0x00, 0xe0, 0x00, 0x08, 0x0a, 0x08, 0xff, 0x08, 0xdd, 0x50, 
	0x0a, 0xa0, 0x2f, 0x35, 0xe0, 0x9b, 0x00, 0x3f, 0x25, 0xe8, 0xc1, 0x00, 0x09, 0xdd, 0xcd, 0x20, 
	0x00, 0x00, 0x04, 0xe6, 0xcc, 0x50, 0x00, 0x2d, 0x4c, 0x85, 0xe0, 0x01, 0xd6, 0x0c, 0x85, 0xe0, 
	0x0c, 0x80, 0x04, 0xdd, 0x60, 0x08, 0x0a, 0x08, 0xff, 0x00, 0x7c, 0xd8, 0x00, 0x00, 0x02, 0xf2, 
	0x5e, 0x00, 0x00, 0x01, 0xe5, 0xa8, 0x00, 0x00, 0x00, 0x6f, 0x70, 0x00, 0x00, 0x07, 0xc6, 0xd3, 
	0x4f, 0x00, 0x2f, 0x20, 0x3d, 0xac, 0x00, 0x2e, 0x50, 0x07, 0xf9, 0x00, 0x05, 0xcc, 0xcc, 0x4d, 
	0x80, 0x03, 0x03, 0x08, 0x03, 0xdf, 0x30, 0x9e, 0x00, 0x6b, 0x00, 0x0a, 0x07, 0x08, 0x01, 0x00, 
	0x03, 0xac, 0x80, 0x00, 0x8d, 0x50, 0x00, 0x06, 0xd2, 0x00, 0x00, 0x0e, 0x60, 0x00, 0x00, 0x3f, 
	0x20, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x0e, 0x60, 0x00, 0x00, 0x06, 0xd2, 0x00, 0x00, 0x00, 
	0x8d, 0x50, 0x00, 0x00, 0x03, 0xac, 0x80, 0x0a, 0x08, 0x08, 0x00, 0x3b, 0xc7, 0x10, 0x00, 0x00, 
	0x18, 0xe4, 0x00, 0x00, 0x00, 0x5e, 0x20, 0x00, 0x00, 0x09, 0xb0, 0x00, 0x00, 0x05, 0xe0, 0x00, 
	0x00, 0x05, 0xe0, 0x00, 0x00, 0x09, 0xb0, 0x00, 0x00, 0x5e, 0x20, 0x00, 0x18, 0xe4, 0x00, 0x3b, 
	0xc8, 0x10, 0x00, 0x05, 0x07, 0x08, 0x01, 0x00, 0x4f, 0x10, 0x00, 0x6c, 0x7f, 0x6d, 0x40, 0x05, 
	0xef, 0xd3, 0x00, 0x7c, 0x8f, 0x6d, 0x40, 0x00, 0x4f, 0x10, 0x00, 0x06, 0x07, 0x06, 0x00, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0xcc, 0xdf, 0xcc, 0xc0, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x05, 0x04, 0x02, 0x02, 0x4c, 0x70, 0x5e, 0xf0, 0x05, 
	0xc0, 0x5b, 0x20, 0x00, 0x00, 0x01, 0x05, 0x04, 0x02, 0xcc, 0xcc, 0xc0, 0x02, 0x03, 0x02, 0x03, 
	0x9a, 0x00, 0xbd, 0x00, 0x08, 0x09, 0x08, 0x00, 0x00, 0x00, 0x00, 0x7a, 0x00, 0x00, 0x00, 0x05, 
	0xc0, 0x00, 0x00, 0x00, 0x3d, 0x10, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x0c, 0x50, 0x00, 
	0x00, 0x00, 0xa8, 0x00, 0x00, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x00, 
	0x08, 0x09, 0x08, 0x00, 0x00, 0x8c, 0xcc, 0x60, 0x00, 0x09, 0xc1, 0x04, 0xf6, 0x00, 0x1e, 0x40, 
	0x0c, 0xcd, 0x00, 0x3f, 0x10, 0xb7, 0x4f, 0x00, 0x3f, 0x1a, 0x80, 0x4f, 0x00, 0x1e, 0xba, 0x00, 
	0x8d, 0x00, 0x09, 0xe1, 0x02, 0xe6, 0x00, 0x00, 0x8c, 0xcc, 0x60, 0x00, 0x08, 0x06, 0x08, 0x01, 
	0x05, 0xcf, 0x10, 0xd9, 0x6f, 0x10, 0x00, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0x00, 
	0x4f, 0x10, 0x00, 0x4f, 0x10, 0xcc, 0xdf, 0xcc, 0x08, 0x08, 0x08, 0x00, 0x19, 0xcc, 0xdc, 0x50, 
	0x12, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x08, 0xc0, 0x00, 0x00, 0x9c, 0x10, 0x00, 0x2c, 0x80, 0x00, 
	0x04, 0xd4, 0x00, 0x00, 0x1e, 0x40, 0x00, 0x00, 0x3f, 0xcc, 0xcc, 0xc1, 0x08, 0x08, 0x08, 0x01, 
	0x9d, 0xcc, 0xc4, 0x00, 0x10, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x9d, 0x00, 0x03, 0xce, 0xd5, 0x00, 
	0x00, 0x00, 0x3c, 0xa0, 0x00, 0x00, 0x04, 0xf0, 0x30, 0x00, 0x1a, 0xb0, 0x9d, 0xcc, 0xc7, 0x00, 
	0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0xaf, 0x10, 0x00, 0x09, 0xdf, 0x10, 0x00, 0x8b, 0x4f, 0x10, 
	0x07, 0xc0, 0x4f, 0x10, 0x6d, 0x10, 0x4f, 0x10, 0xdc, 0xcc, 0xdf, 0xcc, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x08, 0x08, 0x08, 0x00, 0x3f, 0xcc, 0xcc, 0x80, 0x3f, 0x00, 0x00, 0x00, 
	0x3f, 0x00, 0x00, 0x00, 0x2d, 0xcc, 0xa5, 0x00, 0x00, 0x00, 0x4c, 0xa0, 0x00, 0x00, 0x05, 0xf0, 
	0x00, 0x00, 0x1b, 0xb0, 0x2b, 0xcc, 0xc8, 0x00, 0x08, 0x09, 0x08, 0x00, 0x00, 0x5c, 0xcc, 0x90, 
	0x00, 0x06, 0xd3, 0x00, 0x00, 0x00, 0x0e, 0x60, 0x00, 0x00, 0x00, 0x2f, 0x39, 0xcc, 0x91, 0x00, 
	0x3f, 0xd3, 0x00, 0x9c, 0x00, 0x0e, 0x90, 0x00, 0x4f, 0x00, 0x08, 0xd1, 0x00, 0xba, 0x00, 0x00, 
	0x7c, 0xcc, 0x80, 0x00, 0x08, 0x07, 0x08, 0x01, 0xcc, 0xcc, 0xcd, 0xe0, 0x00, 0x00, 0x1c, 0x70, 
	0x00, 0x01, 0xc7, 0x00, 0x00, 0x2d, 0x60, 0x00, 0x01, 0xd6, 0x00, 0x00, 0x0b, 0x80, 0x00, 0x00, 
	0x2f, 0x20, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x08, 0x08, 0x08, 0x00, 0x05, 0xbc, 0xdc, 0x40, 
	0x2f, 0x40, 0x07, 0xe0, 0x1d, 0x90, 0x0a, 0x90, 0x00, 0xce, 0xd7, 0x00, 0x06, 0xd5, 0xae, 0x40, 
	0x2e, 0x40, 0x08, 0xe0, 0x2f, 0x50, 0x07, 0xe0, 0x05, 0xcc, 0xdb, 0x30, 0x08, 0x09, 0x08, 0x00, 
	0x01, 0x9c, 0xcc, 0x60, 0x00, 0x0d, 0x80, 0x03, 0xe6, 0x00, 0x3f, 0x20, 0x00, 0xcd, 0x00, 0x1e, 
	0x70, 0x05, 0xef, 0x00, 0x03, 0xac, 0xc8, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x9c, 0x00, 0x00, 0x00, 
	0x05, 0xe4, 0x00, 0x03, 0xcc, 0xca, 0x30, 0x00, 0x06, 0x03, 0x06, 0x03, 0xbc, 0x00, 0x9a, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0xbd, 0x00, 0x09, 0x04, 0x06, 0x03, 0xbc, 0x00, 0x9a, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x8c, 0x60, 0xae, 0xf0, 0x06, 0xc0, 0x99, 0x10, 0x00, 0x00, 0x06, 0x08, 
	0x06, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x4a, 0xb6, 0x02, 0x8c, 0x82, 0x00, 0x07, 0xd9, 
	0x30, 0x00, 0x00, 0x03, 0x9c, 0x71, 0x00, 0x00, 0x01, 0x6b, 0x03, 0x07, 0x04, 0x00, 0xcc, 0xcc, 
	0xcc, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xc0, 0x06, 0x08, 0x06, 0x00, 0x71, 0x00, 
	0x00, 0x00, 0x6b, 0xa4, 0x00, 0x00, 0x00, 0x28, 0xc8, 0x20, 0x00, 0x03, 0x9d, 0x70, 0x17, 0xc9, 
	0x30, 0x00, 0xc6, 0x10, 0x00, 0x00, 0x08, 0x07, 0x08, 0x01, 0x5c, 0xcc, 0xc5, 0x00, 0x21, 0x00, 
	0x6e, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x0b, 0xa0, 0x00, 0x00, 0xa7, 0x00, 0x00, 0x03, 0xf1, 
	0x00, 0x00, 0x01, 0xb0, 0x00, 0x00, 0x02, 0xd0, 0x00, 0x00, 0x08, 0x0a, 0x08, 0xff, 0x00, 0x5b, 
	0xcc, 0xb4, 0x00, 0x06, 0xea, 0xbc, 0xff, 0x50, 0x0e, 0xd9, 0x04, 0xf9, 0xd0, 0x3f, 0xf2, 0x05, 
	0xf6, 0xf1, 0x3f, 0xe3, 0x2d, 0xf7, 0xe0, 0x0e, 0xac, 0xb3, 0xbc, 0x40, 0x06, 0xe4, 0x00, 0x10, 
	0x00, 0x00, 0x5c, 0xdc, 0x90, 0x00, 0x08, 0x09, 0x08, 0x00, 0x00, 0x0b, 0xe1, 0x00, 0x00, 0x00, 
	0x2e, 0xd7, 0x00, 0x00, 0x00, 0x8a, 0x7d, 0x00, 0x00, 0x00, 0xd4, 0x1e, 0x40, 0x00, 0x05, 0xc0, 
	0x09, 0xa0, 0x00, 0x0b, 0xdc, 0xcc, 0xe1, 0x00, 0x2e, 0x10, 0x00, 0xc7, 0x00, 0x8a, 0x00, 0x00, 
	0x7d, 0x00, 0x08, 0x08, 0x08, 0x00, 0x4f, 0xcc, 0xd7, 0x00, 0x4f, 0x10, 0x5f, 0x00, 0x4f, 0x10, 
	0x9c, 0x00, 0x4f, 0xcd, 0xd3, 0x00, 0x4f, 0x10, 0x2c, 0x80, 0x4f, 0x10, 0x04, 0xe0, 0x4f, 0x10, 
	0x09, 0xc0, 0x4f, 0xcc, 0xda, 0x20, 0x08, 0x08, 0x08, 0x00, 0x00, 0x39, 0xcc, 0xc7, 0x04, 0xe5, 
	0x00, 0x01, 0x0e, 0x70, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x0e, 0x70, 
	0x00, 0x00, 0x04, 0xe5, 0x00, 0x01, 0x00, 0x3a, 0xcc, 0xc7, 0x08, 0x08, 0x08, 0x00, 0x4f, 0xcc, 
	0xc7, 0x00, 0x4f, 0x10, 0x1d, 0x70, 0x4f, 0x10, 0x06, 0xd0, 0x4f, 0x10, 0x04, 0xf0, 0x4f, 0x10, 
	0x04, 0xf0, 0x4f, 0x10, 0x06, 0xd0, 0x4f, 0x10, 0x1d, 0x60, 0x4f, 0xcc, 0xc6, 0x00, 0x08, 0x07, 
	0x08, 0x01, 0x4f, 0xcc, 0xcc, 0x40, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0xcc, 
	0xca, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0xcc, 
	0xcc, 0x40, 0x08, 0x07, 0x08, 0x01, 0x4f, 0xcc, 0xcc, 0x60, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0xcc, 0xcc, 0x10, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x4b, 0xcc, 0xb0, 0x06, 0xe4, 
	0x00, 0x10, 0x0e, 0x60, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x3f, 0x20, 0x4c, 0xd1, 0x0e, 0x60, 
	0x04, 0xf1, 0x06, 0xe3, 0x04, 0xf1, 0x00, 0x4b, 0xcc, 0xc1, 0x08, 0x08, 0x08, 0x00, 0x4f, 0x10, 
	0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0xcc, 0xcd, 0xf1, 0x4f, 0x10, 
	0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x08, 0x06, 
	0x08, 0x01, 0xcc, 0xdf, 0xcc, 0x00, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0x00, 0x4f, 
	0x10, 0x00, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0xcc, 0xdf, 0xcc, 0x08, 0x07, 0x08, 0x00, 0x00, 0x7c, 
	0xdf, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x11, 0x00, 0x8d, 0x00, 0x2b, 0xcc, 0xb2, 0x00, 0x08, 0x08, 
	0x08, 0x00, 0x4f, 0x10, 0x07, 0xc1, 0x4f, 0x10, 0x7c, 0x10, 0x4f, 0x17, 0xc1, 0x00, 0x4f, 0x8d, 
	0x10, 0x00, 0x4f, 0x6e, 0x50, 0x00, 0x4f, 0x14, 0xe6, 0x00, 0x4f, 0x10, 0x4e, 0x60, 0x4f, 0x10, 
	0x04, 0xe7, 0x08, 0x07, 0x08, 0x01, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0xdd, 0xdd, 0x50, 0x08, 0x09, 0x08, 0x00, 0x4f, 0x80, 0x00, 0x8f, 0x10, 0x4f, 
	0xe0, 0x00, 0xef, 0x10, 0x4f, 0xc6, 0x06, 0xbf, 0x10, 0x4f, 0x5d, 0x0c, 0x5f, 0x10, 0x4f, 0x1c, 
	0x99, 0x4f, 0x10, 0x4f, 0x16, 0xf3, 0x4f, 0x10, 0x4f, 0x10, 0x00, 0x4f, 0x10, 0x4f, 0x10, 0x00, 
	0x4f, 0x10, 0x08, 0x08, 0x08, 0x00, 0x4f, 0x50, 0x04, 0xf1, 0x4f, 0xd1, 0x04, 0xf1, 0x4f, 0xa9, 
	0x04, 0xf1, 0x4f, 0x2d, 0x44, 0xf1, 0x4f, 0x15, 0xc4, 0xf1, 0x4f, 0x10, 0xbb, 0xf1, 0x4f, 0x10, 
	0x3e, 0xf1, 0x4f, 0x10, 0x09, 0xf1, 0x08, 0x09, 0x08, 0x00, 0x00, 0x8c, 0xcc, 0x60, 0x00, 0x09, 
	0xc1, 0x02, 0xe6, 0x00, 0x1e, 0x40, 0x00, 0x7d, 0x00, 0x3f, 0x10, 0x00, 0x4f, 0x00, 0x3f, 0x10, 
	0x00, 0x4f, 0x00, 0x1e, 0x40, 0x00, 0x7d, 0x00, 0x09, 0xc1, 0x02, 0xe6, 0x00, 0x00, 0x8c, 0xcc, 
	0x60, 0x00, 0x08, 0x08, 0x08, 0x00, 0x4f, 0xcc, 0xdb, 0x20, 0x4f, 0x10, 0x09, 0xd0, 0x4f, 0x10, 
	0x05, 0xe0, 0x4f, 0x10, 0x2c, 0x80, 0x4f, 0xcc, 0xa5, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x0a, 0x09, 0x08, 0x00, 0x00, 0x8c, 0xcc, 0x60, 0x00, 0x09, 
	0xc1, 0x02, 0xd6, 0x00, 0x1e, 0x40, 0x00, 0x7d, 0x00, 0x3f, 0x10, 0x00, 0x4f, 0x00, 0x3f, 0x20, 
	0x00, 0x4f, 0x00, 0x0e, 0x50, 0x00, 0x8c, 0x00, 0x07, 0xd3, 0x05, 0xe5, 0x00, 0x00, 0x5b, 0xeb, 
	0x40, 0x00, 0x00, 0x00, 0x4c, 0x20, 0x00, 0x00, 0x00, 0x04, 0xbc, 0x80, 0x08, 0x09, 0x08, 0x00, 
	0x4f, 0xcc, 0xdc, 0x40, 0x00, 0x4f, 0x10, 0x08, 0xe0, 0x00, 0x4f, 0x10, 0x05, 0xe0, 0x00, 0x4f, 
	0x10, 0x2c, 0x60, 0x00, 0x4f, 0xcc, 0xe4, 0x00, 0x00, 0x4f, 0x11, 0xcb, 0x00, 0x00, 0x4f, 0x10, 
	0x1d, 0xa0, 0x00, 0x4f, 0x10, 0x02, 0xd8, 0x00, 0x08, 0x08, 0x08, 0x00, 0x04, 0xbc, 0xcc, 0x80, 
	0x2e, 0x40, 0x00, 0x00, 0x2e, 0x60, 0x00, 0x00, 0x05, 0xdc, 0x50, 0x00, 0x00, 0x05, 0xbd, 0x40, 
	0x00, 0x00, 0x07, 0xe0, 0x11, 0x00, 0x08, 0xd0, 0x2b, 0xcc, 0xca, 0x20, 0x08, 0x08, 0x08, 0x00, 
	0xcc, 0xdf, 0xcc, 0xcc, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 
	0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 
	0x08, 0x08, 0x08, 0x00, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 
	0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x3f, 0x10, 0x04, 0xf0, 0x1e, 0x60, 0x09, 0xb0, 
	0x04, 0xcc, 0xca, 0x10, 0x08, 0x09, 0x08, 0x00, 0x8d, 0x00, 0x00, 0x6d, 0x00, 0x2f, 0x40, 0x00, 
	0xc7, 0x00, 0x0b, 0xa0, 0x03, 0xe1, 0x00, 0x05, 0xe1, 0x09, 0xa0, 0x00, 0x00, 0xd7, 0x1e, 0x40, 
	0x00, 0x00, 0x8d, 0x6d, 0x00, 0x00, 0x00, 0x2e, 0xe7, 0x00, 0x00, 0x00, 0x0a, 0xe1, 0x00, 0x00, 
	0x08, 0x09, 0x08, 0xff, 0x0e, 0x40, 0xc9, 0x03, 0xc0, 0x0c, 0x60, 0xed, 0x05, 0xa0, 0x0a, 0x83, 
	0xcf, 0x18, 0x70, 0x07, 0xa6, 0x7c, 0x5a, 0x50, 0x05, 0xca, 0x48, 0x8d, 0x20, 0x03, 0xec, 0x04, 
	0xce, 0x00, 0x00, 0xfc, 0x00, 0xed, 0x00, 0x00, 0xd8, 0x00, 0xba, 0x00, 0x08, 0x09, 0x08, 0x00, 
	0x1d, 0x80, 0x02, 0xe4, 0x00, 0x04, 0xe4, 0x0c, 0x80, 0x00, 0x00, 0x8d, 0xac, 0x00, 0x00, 0x00, 
	0x0c, 0xe2, 0x00, 0x00, 0x00, 0x2e, 0xe6, 0x00, 0x00, 0x00, 0xc9, 0x5e, 0x30, 0x00, 0x08, 0xc0, 
	0x09, 0xc0, 0x00, 0x4e, 0x20, 0x00, 0xd9, 0x00, 0x08, 0x09, 0x08, 0x00, 0x1e, 0x70, 0x00, 0x8c, 
	0x00, 0x04, 0xe3, 0x04, 0xe2, 0x00, 0x00, 0x8d, 0x2d, 0x60, 0x00, 0x00, 0x0c, 0xea, 0x00, 0x00, 
	0x00, 0x04, 0xf1, 0x00, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x00, 
	0x04, 0xf1, 0x00, 0x00, 0x08, 0x07, 0x08, 0x01, 0xcc, 0xcc, 0xce, 0xe0, 0x00, 0x00, 0x4e, 0x60, 
	0x00, 0x02, 0xe8, 0x00, 0x00, 0x1c, 0xb0, 0x00, 0x00, 0xbc, 0x10, 0x00, 0x08, 0xe2, 0x00, 0x00, 
	0x6e, 0x40, 0x00, 0x00, 0xee, 0xcc, 0xcc, 0xc0, 0x0b, 0x07, 0x08, 0x02, 0x3f, 0xcc, 0xcc, 0x30, 
	0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 
	0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 
	0x3f, 0x00, 0x00, 0x00, 0x2d, 0xcc, 0xcc, 0x30, 0x08, 0x09, 0x08, 0x00, 0x5c, 0x00, 0x00, 0x00, 
	0x00, 0x08, 0xa0, 0x00, 0x00, 0x00, 0x00, 0xa7, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x50, 0x00, 0x00, 
	0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x10, 0x00, 0x00, 0x00, 0x05, 0xc0, 0x00, 0x00, 
	0x00, 0x00, 0x8a, 0x00, 0x0b, 0x07, 0x08, 0x00, 0x6c, 0xcc, 0xdf, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x6c, 0xcc, 0xcd, 0x10, 0x06, 0x08, 0x08, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x09, 0xd0, 0x00, 
	0x00, 0x3c, 0x98, 0x00, 0x00, 0xc3, 0x0d, 0x30, 0x08, 0x90, 0x04, 0xc0, 0x3d, 0x00, 0x00, 0x97, 
	0x01, 0x07, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xc0, 0x02, 0x04, 0x08, 0x02, 0x4d, 0x30, 0x02, 0xc3, 
	0x06, 0x08, 0x06, 0x00, 0x00, 0x8d, 0xcd, 0xf1, 0x09, 0xb1, 0x04, 0xf1, 0x1e, 0x30, 0x04, 0xf1, 
	0x3f, 0x10, 0x05, 0xf1, 0x1e, 0x50, 0x4d, 0xf1, 0x06, 0xdc, 0x93, 0xf3, 0x08, 0x08, 0x08, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x3a, 0xcc, 0x30, 0x4f, 0xc2, 0x08, 0xd0, 
	0x4f, 0x30, 0x04, 0xf1, 0x4f, 0x10, 0x06, 0xd0, 0x4f, 0x10, 0x2d, 0x60, 0x4f, 0xcc, 0xc6, 0x00, 
	0x06, 0x07, 0x06, 0x00, 0x00, 0x8d, 0xcc, 0x70, 0x0b, 0xa0, 0x00, 0x10, 0x3f, 0x20, 0x00, 0x00, 
	0x3f, 0x20, 0x00, 0x00, 0x0b, 0xa0, 0x00, 0x00, 0x00, 0x8c, 0xcc, 0x80, 0x08, 0x08, 0x08, 0x00, 
	0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x7c, 0xcd, 0xf1, 0x09, 0xb1, 0x04, 0xf1, 
	0x1e, 0x30, 0x04, 0xf1, 0x3f, 0x10, 0x05, 0xf1, 0x1e, 0x50, 0x4d, 0xf1, 0x05, 0xdc, 0x95, 0xf1, 
	0x06, 0x08, 0x06, 0x00, 0x01, 0x9c, 0xcb, 0x20, 0x0b, 0x80, 0x07, 0xc0, 0x2f, 0xcc, 0xcc, 0xd0, 
	0x2f, 0x20, 0x00, 0x00, 0x0b, 0xa0, 0x00, 0x30, 0x01, 0x9d, 0xcc, 0x90, 0x08, 0x07, 0x08, 0x01, 
	0x00, 0x3b, 0xcc, 0xd0, 0x01, 0xe6, 0x00, 0x00, 0x03, 0xf1, 0x00, 0x00, 0xcd, 0xfc, 0xcc, 0x00, 
	0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 
	0x08, 0x08, 0x06, 0x00, 0x00, 0x7c, 0xcd, 0xf1, 0x09, 0xb1, 0x04, 0xf1, 0x1e, 0x30, 0x04, 0xf1, 
	0x3f, 0x10, 0x05, 0xf1, 0x1e, 0x50, 0x4d, 0xf1, 0x05, 0xdc, 0x95, 0xf0, 0x11, 0x00, 0x09, 0xb0, 
	0x2a, 0xcc, 0xc9, 0x10, 0x08, 0x08, 0x08, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 
	0x4f, 0x3a, 0xcd, 0x60, 0x4f, 0xc3, 0x06, 0xe0, 0x4f, 0x30, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 
	0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x08, 0x07, 0x08, 0x00, 0x00, 0x1a, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x3c, 0xcf, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 
	0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0xcc, 0x10, 0x0a, 0x07, 0x08, 0x00, 
	0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xcc, 0xdf, 0x10, 0x00, 0x00, 0x4f, 0x10, 
	0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x00, 
	0x00, 0x00, 0x9c, 0x00, 0x3c, 0xcc, 0xa2, 0x00, 0x08, 0x08, 0x08, 0x01, 0x4f, 0x10, 0x00, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x1b, 0xa0, 0x4f, 0x14, 0xd6, 0x00, 0x4f, 0x8e, 0x30, 0x00, 
	0x4f, 0x4d, 0x80, 0x00, 0x4f, 0x11, 0xca, 0x00, 0x4f, 0x10, 0x0b, 0xc1, 0x08, 0x07, 0x08, 0x00, 
	0x5c, 0xcf, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 
	0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0xcc, 0x40, 
	0x06, 0x09, 0x06, 0x00, 0x4f, 0x6c, 0xc4, 0xcb, 0x00, 0x4f, 0x84, 0xf8, 0x4f, 0x10, 0x4f, 0x24, 
	0xf2, 0x4f, 0x10, 0x4f, 0x14, 0xf1, 0x4f, 0x10, 0x4f, 0x14, 0xf1, 0x4f, 0x10, 0x4f, 0x14, 0xf1, 
	0x4f, 0x10, 0x06, 0x08, 0x06, 0x00, 0x4f, 0x3a, 0xcd, 0x60, 0x4f, 0xc3, 0x06, 0xe0, 0x4f, 0x30, 
	0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 0x06, 0x09, 
	0x06, 0x00, 0x01, 0x9c, 0xcc, 0x70, 0x00, 0x0c, 0x90, 0x01, 0xc9, 0x00, 0x3f, 0x20, 0x00, 0x5e, 
	0x00, 0x3f, 0x20, 0x00, 0x5e, 0x00, 0x0c, 0x90, 0x01, 0xb9, 0x00, 0x01, 0x9c, 0xcc, 0x70, 0x00, 
	0x08, 0x08, 0x06, 0x00, 0x4f, 0x3a, 0xcc, 0x30, 0x4f, 0xc2, 0x08, 0xd0, 0x4f, 0x30, 0x04, 0xf1, 
	0x4f, 0x10, 0x06, 0xd0, 0x4f, 0x10, 0x2d, 0x60, 0x4f, 0xcc, 0xc6, 0x00, 0x4f, 0x10, 0x00, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x08, 0x08, 0x06, 0x00, 0x00, 0x7c, 0xcd, 0xf1, 0x09, 0xb1, 0x04, 0xf1, 
	0x1e, 0x30, 0x04, 0xf1, 0x3f, 0x10, 0x05, 0xf1, 0x1e, 0x50, 0x4d, 0xf1, 0x05, 0xdc, 0x95, 0xf1, 
	0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x06, 0x07, 0x06, 0x01, 0x4f, 0x4a, 0xcf, 0x10, 
	0x4f, 0xb1, 0x4f, 0x10, 0x4f, 0x20, 0x02, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 
	0x4f, 0x10, 0x00, 0x00, 0x06, 0x07, 0x06, 0x01, 0x4c, 0xcc, 0xc6, 0x00, 0xea, 0x00, 0x00, 0x00, 
	0x6d, 0xb6, 0x20, 0x00, 0x00, 0x48, 0xd9, 0x00, 0x40, 0x00, 0x6e, 0x00, 0x9c, 0xcc, 0xc5, 0x00, 
	0x08, 0x07, 0x08, 0x01, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0xcd, 0xfc, 0xcc, 0xc0, 
	0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x04, 0xf1, 0x00, 0x00, 0x02, 0xf5, 0x00, 0x10, 
	0x00, 0x5c, 0xcc, 0xb0, 0x06, 0x08, 0x06, 0x00, 0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x04, 0xf1, 
	0x4f, 0x10, 0x04, 0xf1, 0x4f, 0x10, 0x06, 0xf1, 0x2f, 0x30, 0x5d, 0xf1, 0x08, 0xdc, 0x95, 0xf1, 
	0x06, 0x09, 0x06, 0x00, 0x7c, 0x00, 0x00, 0x6c, 0x00, 0x0d, 0x60, 0x00, 0xd4, 0x00, 0x06, 0xd0, 
	0x08, 0xb0, 0x00, 0x00, 0xc7, 0x1e, 0x20, 0x00, 0x00, 0x4e, 0xa9, 0x00, 0x00, 0x00, 0x0b, 0xe1, 
	0x00, 0x00, 0x06, 0x09, 0x06, 0x00, 0xb6, 0x08, 0xd0, 0x0d, 0x10, 0x89, 0x0c, 0xb4, 0x1d, 0x00, 
	0x5c, 0x3a, 0x6a, 0x4a, 0x00, 0x2e, 0x95, 0x1e, 0x87, 0x00, 0x0e, 0xe1, 0x0b, 0xe4, 0x00, 0x0a, 
	0xb0, 0x05, 0xf1, 0x00, 0x06, 0x09, 0x06, 0x00, 0x1c, 0x90, 0x05, 0xd2, 0x00, 0x01, 0xc9, 0x5d, 
	0x20, 0x00, 0x00, 0x1d, 0xe3, 0x00, 0x00, 0x00, 0x4d, 0xd8, 0x00, 0x00, 0x03, 0xe4, 0x2d, 0x80, 
	0x00, 0x3e, 0x40, 0x02, 0xd7, 0x00, 0x08, 0x08, 0x06, 0x00, 0x8c, 0x00, 0x00, 0x99, 0x0c, 0x70, 
	0x03, 0xe1, 0x03, 0xe2, 0x0b, 0x70, 0x00, 0x9c, 0x4d, 0x00, 0x00, 0x0d, 0xe5, 0x00, 0x00, 0x08, 
	0xc0, 0x00, 0x00, 0x3d, 0x20, 0x00, 0xcc, 0xb3, 0x00, 0x00, 0x06, 0x06, 0x06, 0x01, 0xcc, 0xcc, 
	0xee, 0x00, 0x04, 0xe4, 0x00, 0x4e, 0x40, 0x04, 0xe4, 0x00, 0x4e, 0x40, 0x00, 0xee, 0xcc, 0xcc, 
	0x0b, 0x07, 0x08, 0x01, 0x00, 0x09, 0xcc, 0x80, 0x00, 0x3f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 
	0x00, 0x4f, 0x10, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x3c, 0xe3, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 
	0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x3f, 0x20, 0x00, 0x00, 0x08, 0xcc, 0x80, 
	0x08, 0x03, 0x08, 0x03, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 0x4f, 0x10, 
	0x4f, 0x10, 0x4f, 0x10, 0x0b, 0x07, 0x08, 0x00, 0x3c, 0xd8, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x00, 
	0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x2e, 0x40, 0x00, 0x00, 0x05, 0xec, 0x80, 
	0x00, 0x1e, 0x40, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x10, 0x00, 0x00, 0x4f, 0x00, 0x00, 
	0x3c, 0xc7, 0x00, 0x00, 0x02, 0x09, 0x04, 0x00, 0x08, 0xcc, 0x60, 0x5d, 0x00, 0x2d, 0x20, 0x7b, 
	0xc5, 0x00, 
};

inline float CXY(float r, float d)
{
	float result;
	asm("sqrt.s %0, %1": "=f"(result): "f"(r*r - d*d));
	return result;
}

inline u32 alphaColor(u8 a, u32 color1, u32 color2)
{
	return (u32)((u8)(R(color1) * a/255.0f + R(color2) * (255 - a)/255.0f) | ((u8)(G(color1) * a/255.0f + G(color2) * (255 - a)/255.0f) << 8) | ((u8)(B(color1) * a/255.0f + B(color2) * (255 - a)/255.0f) << 16));
}

static void setXY(Point * p, u16 x, u16 y)
{
	p->x = x;
	p->y = y;
}

extern void setP0(u16 x, u16 y)
{
	setXY(&p0, x, y);
}

extern void setP1(u16 x, u16 y)
{
	setXY(&p1, x, y);
}

extern void setP(u16 x0, u16 y0, u16 x1, u16 y1)
{
	setXY(&p0, x0, y0);
	setXY(&p1, x1, y1);
}

extern void setGcolor(u32 color, int offset)
{
	Gcolor[offset] = color;
}

extern u32 getGcolor(int offset)
{
	return Gcolor[offset];
}

static void initblur();
static bool getPixelColor(Point * p, u32 * color);

extern bool initGraphic(SceUID id, void * gvram)
{
	heapid = id;
	sceDisplayGetFrameBuf(&vram, &bufferwidth, &pixelformat, unk);
	vram = gvram;
	if(pixelformat == PSP_DISPLAY_PIXEL_FORMAT_8888)
	{
		pixelsize = 4;
	}
	else
	{
		pixelsize = 2;
	}
	buffersize = bufferwidth * SCREEN_HEIGHT * pixelsize;
	vram_buf = sceKernelAllocHeapMemory(heapid, buffersize);
	memcpy(vram_buf, vram, buffersize);
	
	initblur();
	
	return true;
}

extern void restore()
{
	sceDisplayWaitVblankStart();
	memcpy(vram, vram_buf, buffersize);
}

extern void freeGraphic()
{
	restore();
	sceKernelFreeHeapMemory(heapid, r);
	sceKernelFreeHeapMemory(heapid, g);	
	sceKernelFreeHeapMemory(heapid, b);
	sceKernelFreeHeapMemory(heapid, vram_buf);
}

static void * Pixel(Point * p)
{
	if(p->x >= SCREEN_WIDTH || p->y >= SCREEN_HEIGHT)
		return NULL;
	return (void *)((u32)vram + (p->x + p->y * bufferwidth) * pixelsize);
}

static bool getPixelColor(Point * p, u32 * color)
{
	const u16 * color16 = (const u16 *)Pixel(p);
	if(!color16)
	{
		return false;
	}
	switch (pixelformat)
	{
		case PSP_DISPLAY_PIXEL_FORMAT_565:
			*color = ((*color16 & 0x1f) << 3) | (((*color16 >> 5) & 0x3f) << 10) | (((*color16 >> 11) & 0x1f) << 19);
			break;
		case PSP_DISPLAY_PIXEL_FORMAT_5551:
			*color = ((*color16 & 0x1f) << 3) | (((*color16 >> 5) & 0x1f) << 11) | (((*color16 >> 10) & 0x1f) << 19);
			break;
		case PSP_DISPLAY_PIXEL_FORMAT_4444:
			*color = ((*color16 & 0xf) << 4) | (((*color16 >> 4) & 0xf) << 12) | (((*color16 >> 8) & 0xf) << 20);
			break;
		case PSP_DISPLAY_PIXEL_FORMAT_8888:
			*color = *((u32 *)Pixel(p));
			break;
	}
	return true;
}

static bool putPixelColor(u32 color, Point * p)
{
	if(p->x >= SCREEN_WIDTH || p->y >= SCREEN_HEIGHT)
		return false;
	int a = 0;
	if (IS_ALPHA(color))
	{
		a = A(color);
		if (a == 0)
			return true;
		u32 color_tmp = 0;
		getPixelColor(p, &color_tmp);
		color = alphaColor(a, color, color_tmp);
	}
	if(pixelformat == PSP_DISPLAY_PIXEL_FORMAT_8888)
	{
		*((u32 *)Pixel(p)) = color;
	}
	else
	{
		u16 color16 = 0;
		switch (pixelformat)
		{
			case PSP_DISPLAY_PIXEL_FORMAT_565:
				color16 = (R(color) >> 3) | ((G(color) >> 2) << 5) | ((B(color) >> 3) << 11);
				break;
			case PSP_DISPLAY_PIXEL_FORMAT_5551:
				color16 = (R(color) >> 3) | ((G(color) >> 3) << 5) | ((B(color) >> 3) << 10);
				break;
			case PSP_DISPLAY_PIXEL_FORMAT_4444:
				color16 = (R(color) >> 4) | ((G(color) >> 4) << 4) | ((B(color) >> 4) << 8);
				break;
		}
		*((u16 *)Pixel(p)) = color16;
	}
	return true;
}

static bool clearPixel(Point * p)
{
	if(p->x >= SCREEN_WIDTH || p->y >= SCREEN_HEIGHT)
		return false;
	memcpy(Pixel(p), (void *)((u32)vram_buf + (p->x + p->y * bufferwidth) * pixelsize), pixelsize);
	return true;
}

extern void drawLine()
{
	Point pen;
	int dy = p1.y - p0.y, dx = p1.x - p0.x, stepx, stepy;
	
	pen.x = p0.x;
	pen.y = p0.y;
	
	if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
	if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
	dy <<= 1;
	dx <<= 1;
	
	putPixelColor(Gcolor[2], &pen);
	if (dx > dy) {
		int fraction = dy - (dx >> 1);
		while (pen.x != p1.x) {
			if (fraction >= 0) {
				pen.y += stepy;
				fraction -= dx;
			}
			pen.x += stepx;
			fraction += dy;
			putPixelColor(Gcolor[2], &pen);
		}
	} else {
		int fraction = dx - (dy >> 1);
		while (pen.y != p1.y) {
			if (fraction >= 0) {
				pen.x += stepx;
				fraction -= dy;
			}
			pen.y += stepy;
			fraction += dx;
			putPixelColor(Gcolor[2], &pen);
		}
	}
}

static float inRect(Point * point, int r, u32 flag)
{
	if(point->x < p0.x || point->x > p1.x)
	{
		return 1;
	}
	else
	{
		float y0, y1;
		Point cc;
		if(point->x < (p0.x + r))
		{
			setXY(&cc, p0.x + r, p0.y + r);
			y0 = (flag & CORNER_LT)? (cc.y - CXY(r, cc.x - point->x)): p0.y;
			setXY(&cc, p0.x + r, p1.y - r);
			y1 = (flag & CORNER_LB)? (cc.y + CXY(r, cc.x - point->x)): p1.y;
		}
		else if(point->x > (p1.x - r))
		{
			setXY(&cc, p1.x - r, p0.y + r);
			y0 = (flag & CORNER_RT)? (cc.y - CXY(r, point->x - cc.x)): p0.y;
			setXY(&cc, p1.x - r, p1.y - r);
			y1 = (flag & CORNER_RB)? (cc.y + CXY(r, point->x - cc.x)): p1.y;
		}
		else
		{
			y0 = p0.y;
			y1 = p1.y;
		}
		if(point->y < y0)
		{
			return (y0 - point->y) > 1? 1: (y0 - point->y);
		}
		else if(point->y > y1)
		{
			return (point->y - y1) > 1? 1: (point->y - y1);
		}
	}
	return 0;
}

static bool blurPixel(Point * p)
{
	if(p->x >= SCREEN_WIDTH || p->y >= SCREEN_HEIGHT)
		return false;
	int sumr = 0, sumg = 0, sumb = 0;
	int k, offset;
	for(k = 0; k < gauss_width; k ++)
	{
		offset = SCREEN_WIDTH * (p->y - ((gauss_width - 1) >> 1) + k) + p->x;
  		sumr += r[offset] * gauss_fact[k];
  		sumg += g[offset] * gauss_fact[k];
  		sumb += b[offset] * gauss_fact[k];
	}
	putPixelColor(0xFF000000 | (u8)(sumb/gauss_sum) << 16 | (u8)(sumg/gauss_sum) << 8 | (u8)(sumr/gauss_sum), p);
	return true;
}

extern void drawRect_p(int r, bool clear, u32 flag, Point * pt0, Point * pt1)
{
	u32 tcolor;
	Point tpen, cc, pen;
	int y0, y1, sw = A(flag), i;
	float Yn, Yh, tmp;
	for(pen.x = pt0->x; pen.x <= pt1->x; pen.x ++)
	{
		if(pen.x < (pt0->x + r))
		{
			setXY(&cc, pt0->x + r, pt0->y + r);
			Yn = cc.y - CXY(r, cc.x - pen.x);
			Yh = cc.y - CXY(r, cc.x - (pen.x + 1));
			y0 = (flag & CORNER_LT)? (int)Yn + IS_FLOAT(Yn) : pt0->y;
			y1 = (flag & CORNER_LB)? (int)(pt1->y - r + CXY(r, cc.x - pen.x)): pt1->y;
			while(Yn > Yh && (flag & CORNER_LT))
			{
				tmp = IS_FLOAT(Yn)? ((int)Yn + 1 - Yn): 0;
				tmp += ((((IS_FLOAT(Yn)? ((int)Yn + 1): Yn) - Yh) > 1? (pen.x + 1 - cc.x + CXY(r, cc.y - (IS_FLOAT(Yn)? (int)Yn: (Yn - 1)))): 0) + (pen.x + 1 - cc.x + CXY(r, cc.y - Yn))) * (((IS_FLOAT(Yn)? ((int)Yn + 1): Yn) - Yh) > 1? (1 - tmp): (Yn - Yh)) * 0.5f;
				tcolor = (((u8)(A(Gcolor[0]) * tmp)) << 24) | BGR(Gcolor[0]);
				Yn = IS_FLOAT(Yn)? (int)Yn: (Yn - 1);
				setXY(&tpen, pen.x, (u16)Yn);
				//if(clear) blurPixel(&tpen);
				putPixelColor(tcolor, &tpen);
			}
		}
		else if(pen.x > (pt1->x - r))
		{
			cc.x = pt1->x - r + 1;
			cc.y = pt0->y + r;
			Yh = cc.y - CXY(r, pen.x - cc.x);
			Yn = cc.y - CXY(r, pen.x + 1 - cc.x);
			y0 = (flag & CORNER_RT)? (int)Yn + IS_FLOAT(Yn): pt0->y;
			y1 = (flag & CORNER_RB)? (int)(pt1->y - r + CXY(r, pen.x + 1 - cc.x)): pt1->y;
			while(Yn > Yh  && (flag & CORNER_RT))
			{
				tmp = IS_FLOAT(Yn)? ((int)Yn + 1 - Yn): 0;
				tmp += ((((IS_FLOAT(Yn)? ((int)Yn + 1): Yn) - Yh) > 1? (cc.x + CXY(r, cc.y - (IS_FLOAT(Yn)? (int)Yn: (Yn - 1))) - pen.x): 0) + (cc.x + CXY(r, cc.y - Yn) - pen.x)) * (((IS_FLOAT(Yn)? ((int)Yn + 1): Yn) - Yh) > 1? (1 - tmp): (Yn - Yh)) * 0.5f;
				tcolor = (((u8)(A(Gcolor[0]) * tmp)) << 24) | BGR(Gcolor[0]);
				Yn = IS_FLOAT(Yn)? (int)Yn: (Yn - 1);
				tpen.x = pen.x;
				tpen.y = (u16)Yn;
				//if(clear) blurPixel(&tpen);
				putPixelColor(tcolor, &tpen);
				if(sw && (pen.x + sw > pt1->x))
				{
					for(i = sw; i > 0; i --)
					{
						tpen.x ++;
						tpen.y ++;
						putPixelColor(((((u8)(i * inRect(&tpen, r, flag) * tmp * A(Gcolor[4]))) << 24) | BGR(Gcolor[4])), &tpen);
					}
				}
			}
		}
		else
		{
			y0 = pt0->y;
			y1 = pt1->y;
		}
		for(pen.y = y0; pen.y <= y1; pen.y ++)
		{
			if(clear) blurPixel(&pen);
			putPixelColor(Gcolor[0], &pen);
			if((sw) && ((pen.y + sw > y1) || (pen.x + sw > pt1->x)))
			{
				tpen.x = pen.x;
				tpen.y = pen.y;
				for(i = sw; i > 0; i --)
				{
					tpen.x ++;
					tpen.y ++;
					putPixelColor(((((u8)(i * inRect(&tpen, r, flag) * A(Gcolor[4]))) << 24) | BGR(Gcolor[4])), &tpen);
				}
			}
		}
		if(pen.x < (pt0->x + r) && (flag & CORNER_LB))
		{
			cc.x = pt0->x + r;
			cc.y = pt1->y - r + 1;
			Yn = cc.y + CXY(r, cc.x - pen.x);
			Yh = cc.y + CXY(r, cc.x - (pen.x + 1));
			while(Yn < Yh)
			{
				tmp = IS_FLOAT(Yn)? (Yn - (int)Yn): 0;
				tmp += (((Yh - (int)Yn) > 1? (pen.x + 1 - cc.x + CXY(r, (int)Yn + 1 - cc.y)): 0) + (pen.x + 1 - cc.x + CXY(r, Yn - cc.y))) * ((Yh - (int)Yn) > 1? (1 - tmp): (Yh - Yn)) * 0.5f;
				tcolor = (((u8)(A(Gcolor[0]) * tmp)) << 24) | BGR(Gcolor[0]);
				tpen.x = pen.x;
				tpen.y = (int)Yn;
				//if(clear) blurPixel(&tpen);
				putPixelColor(tcolor, &tpen);
				if(sw)
				{
					for(i = sw; i > 0; i --)
					{
						tpen.x ++;
						tpen.y ++;
						putPixelColor(((((u8)(i * inRect(&tpen, r, flag) * tmp * A(Gcolor[4]))) << 24) | BGR(Gcolor[4])), &tpen);
					}
				}
				Yn = (int)Yn + 1;
			}
		}
		else if(pen.x > (pt1->x - r) && (flag & CORNER_RB))
		{
			cc.x = pt1->x - r + 1;
			cc.y = pt1->y - r + 1;
			Yh = cc.y + CXY(r, pen.x - cc.x);
			Yn = cc.y + CXY(r, pen.x + 1 - cc.x);
			while(Yn < Yh)
			{
				tmp = IS_FLOAT(Yn)? (Yn - (int)Yn): 0;
				tmp += (((Yh - (int)Yn) > 1? (cc.x + CXY(r, (int)Yn + 1 - cc.y) - pen.x): 0) + (cc.x + CXY(r, Yn - cc.y) - pen.x)) * ((Yh - (int)Yn) > 1? (1 - tmp): (Yh - Yn)) * 0.5f;
				tcolor = (((u8)(A(Gcolor[0]) * tmp)) << 24) | BGR(Gcolor[0]);
				tpen.x = pen.x;
				tpen.y = (int)Yn;
				//if(clear) blurPixel(&tpen);
				putPixelColor(tcolor, &tpen);
				if(sw)
				{
					for(i = sw; i > 0; i --)
					{
						tpen.x ++;
						tpen.y ++;
						putPixelColor(((((u8)(i * inRect(&tpen, r, flag) * tmp * A(Gcolor[4]))) << 24) | BGR(Gcolor[4])), &tpen);
					}
				}
				Yn = (int)Yn + 1;
			}
		}
	}
}

extern void drawRect(int r, bool clear, u32 flag)
{
	drawRect_p(r, clear, flag, &p0, &p1);
}

extern void clearRect(u32 color)
{
	Point pen;
	for(pen.x = p0.x; pen.x <= p1.x; pen.x++)
	{
		for(pen.y = p0.y; pen.y <= p1.y; pen.y++)
		{
			clearPixel(&pen);
			if(color) putPixelColor(color, &pen);
		}
	}
}

static enum DrawCharError drawChar(char cht, Point * pen, bool flag, bool ignore)
{
	if(pen->x + 8 > p1.x && !ignore)
	{
		return ENDLINE;
	}
	if(pen->y > p1.y && !ignore)
	{
		return ENDPAGE;
	}
	if(cht == 0x0A || cht == 0x0D)
	{
		return NEWLINE;
	}

	u8 ch = cht - 0x20;

	Point tpen;
	u8 g[2];
	int wid, i, j, c, d;
	u8 gray;

	wid = font[fontp[ch] + 1] / 2 + ((font[fontp[ch] + 1] % 2)? 1: 0);
	for(i = 0; i < font[fontp[ch]]; i++)
	{
		for(j = 0; j < font[fontp[ch] + 1]; j+=2)
		{
			gray = font[fontp[ch] + 4 + i * wid + j/2];
			g[0] = (gray & 0xF0) >> 4;
			g[1] = (gray & 0x0F);
			for(c = 0; c < 2 && (j + c) < font[fontp[ch] + 1]; c++)
			{
				if(g[c] > 0)
				{
					tpen.x = pen->x + font[fontp[ch] + 3] + j + c;
					tpen.y = pen->y - font[fontp[ch] + 2] + i;
					putPixelColor(((((u8)(g[c] * 17) * A(Gcolor[1])/255) << 24) | BGR(Gcolor[1])), &tpen);
					for(d = 3; d > 0 && flag; d --)
					{
						tpen.x ++;
						tpen.y ++;
						putPixelColor(((((u8)(g[c] * d) * A(Gcolor[3])/255) << 24) | BGR(Gcolor[3])), &tpen);
					}
				}
			}
		}
	}
	pen->x += 8;
	return DRAWED;
}

extern int drawString_p(const char * txt, u16 flag, Point * pt0)
{
	Point pen;
	enum DrawCharError error;
	pen.x = pt0->x;
	pen.y = pt0->y;
	int n = 0, nl = 0;
	bool shadow = false;
	bool endline = flag & FENDLINE;
	if(flag & FSHADOW)
	{
		shadow = true;
	}
	while(txt[n] > 0)
	{
		error = drawChar(txt[n], &pen, shadow, !endline);
		if(flag & FTHICK)
		{
			pen.x -= 7;
			error = drawChar(txt[n], &pen, shadow, true);
		}
		if(error == ENDLINE || error == NEWLINE)
		{
			nl ++;
			pen.x = pt0->x;
			pen.y = pt0->y + line_height * nl;
			if(error == NEWLINE)
			{
				n ++;
			}
		}
		else if(error == ENDPAGE)
		{
			return n;
		}
		else
		{
			n ++;
		}
	}
	return -1;
}

extern int drawString(const char * txt, u16 flag)
{
	return drawString_p(txt, flag, &p0);
}

static void initblur()
{
	Point pen;
	int sumr = 0,
		sumg = 0,
		sumb = 0;
	int i, j, k;
	u32 color = 0;
	int width = SCREEN_WIDTH,
		height = SCREEN_HEIGHT;
	int offset;
	r = (u8 *)sceKernelAllocHeapMemory(heapid, width * height * sizeof(u8));
	g = (u8 *)sceKernelAllocHeapMemory(heapid, width * height * sizeof(u8));
	b = (u8 *)sceKernelAllocHeapMemory(heapid, width * height * sizeof(u8));
	for(i = 1 ; i < SCREEN_WIDTH - 2; i ++)
	{
		for(j = 1; j < SCREEN_HEIGHT - 2; j ++)
		{
	    	sumr = sumg = sumb = 0;
	    	for(k = 0; k < gauss_width; k ++)
			{
				setXY(&pen, i - ((gauss_width - 1) >> 1) + k, j);
				getPixelColor(&pen, &color);
	      		sumr += R(color) * gauss_fact[k];
	      		sumg += G(color) * gauss_fact[k];
	      		sumb += B(color) * gauss_fact[k];
	    	}
			offset = width * j + i;
			r[offset] = sumr / gauss_sum;
			g[offset] = sumg / gauss_sum;
			b[offset] = sumb / gauss_sum;
		}
	}
}

extern void blur()
{
	Point pen;
	for(pen.x = p0.x ; pen.x < p1.x; pen.x ++)
	{
		for(pen.y = p0.y; pen.y < p1.y; pen.y ++)
		{
			blurPixel(&pen);
		}
	}
}
